package com.lighthouse.doc.operate.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lighthouse.doc.base.dao.KnowledgeDocBaseInfoRepository;
import com.lighthouse.doc.base.domain.KnowledgeDocBaseInfo;
import com.lighthouse.doc.operate.domain.KnowledgeDocOperateDTO;
import com.lighthouse.repo.domain.KnowledgeRepoBaseInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lighthouse.doc.operate.mapper.KnowledgeDocOperateMapper;
import com.lighthouse.doc.operate.domain.KnowledgeDocOperate;
import com.lighthouse.doc.operate.service.IKnowledgeDocOperateService;

/**
 * 用户-文档操作关联Service业务层处理
 * 
 * @author ysz
 * @date 2023-04-26
 */
@Service
public class KnowledgeDocOperateServiceImpl implements IKnowledgeDocOperateService 
{
    @Autowired
    private KnowledgeDocOperateMapper knowledgeDocOperateMapper;

    @Autowired
    private KnowledgeDocBaseInfoRepository knowledgeDocBaseInfoRepository;

    private static final String LIKE = "like";

    private static final String COLLECT = "collect";

    /**
     * 查询用户-文档操作关联
     * 
     * @param docId 用户-文档操作关联主键
     * @return 用户-文档操作关联
     */
    @Override
    public KnowledgeDocOperate selectKnowledgeDocOperateByDocAndUser(String docId)
    {
        return knowledgeDocOperateMapper.selectKnowledgeDocOperateByDocAndUser(docId, SecurityUtils.getUserId());
    }

    /**
     * 查询用户-文档操作关联列表
     * 
     * @param knowledgeDocOperate 用户-文档操作关联
     * @return 用户-文档操作关联
     */
    @Override
    public List<KnowledgeDocOperate> selectKnowledgeDocOperateList(KnowledgeDocOperate knowledgeDocOperate)
    {
        return knowledgeDocOperateMapper.selectKnowledgeDocOperateList(knowledgeDocOperate);
    }

    /**
     * 新增用户-文档操作关联
     * 
     * @param knowledgeDocOperateDTO 用户-文档操作关联
     * @return 结果
     */
    @Override
    public int insertKnowledgeDocOperate(KnowledgeDocOperateDTO knowledgeDocOperateDTO)
    {
        syncOperateToES(knowledgeDocOperateDTO);
        KnowledgeDocOperate knowledgeDocOperate = KnowledgeDocOperate.builder().build().copyDTOInfo(knowledgeDocOperateDTO);
        // 没有就新增，有就更新
        List<KnowledgeDocOperate> operates = knowledgeDocOperateMapper.selectKnowledgeDocOperateList(KnowledgeDocOperate.builder()
                .docId(knowledgeDocOperate.getDocId())
                .userId(knowledgeDocOperate.getUserId())
                .build());
        knowledgeDocOperate.setOperateTime(new Date());
        if (operates.size() > 0) {
            return knowledgeDocOperateMapper.updateKnowledgeDocOperate(knowledgeDocOperate);
        } else {
            return knowledgeDocOperateMapper.insertKnowledgeDocOperate(knowledgeDocOperate);
        }
    }

    /**
     * 同步ES点赞、收藏等操作的数据量
     */
    private void syncOperateToES(KnowledgeDocOperateDTO knowledgeDocOperateDTO) {
        KnowledgeDocBaseInfo knowledgeDocBaseInfo = knowledgeDocBaseInfoRepository.findById(knowledgeDocOperateDTO.getDocId()).orElse(null);
        if (LIKE.equals(knowledgeDocOperateDTO.getOperateType())) {
            int i = 1;
            if (!"Y".equals(knowledgeDocOperateDTO.getIsLike()) && knowledgeDocBaseInfo.getDocLikes() > 0) {
                // 防止减出负数
                i = -1;
            }
            knowledgeDocBaseInfo.setDocLikes(knowledgeDocBaseInfo.getDocLikes() + i);
        } else if (COLLECT.equals(knowledgeDocOperateDTO.getOperateType())) {
            int i = 1;
            if (!"Y".equals(knowledgeDocOperateDTO.getIsCollect()) && knowledgeDocBaseInfo.getDocCollects() > 0) {
                // 防止减出负数
                i = -1;
            }
            knowledgeDocBaseInfo.setDocCollects(knowledgeDocBaseInfo.getDocCollects() + i);
        }
        knowledgeDocBaseInfoRepository.save(knowledgeDocBaseInfo);
    }

    /**
     * 修改用户-文档操作关联
     * 
     * @param knowledgeDocOperate 用户-文档操作关联
     * @return 结果
     */
    @Override
    public int updateKnowledgeDocOperate(KnowledgeDocOperate knowledgeDocOperate)
    {
        return knowledgeDocOperateMapper.updateKnowledgeDocOperate(knowledgeDocOperate);
    }

    /**
     * 批量删除用户-文档操作关联
     * 
     * @param docIds 需要删除的用户-文档操作关联主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeDocOperateByDocIds(String[] docIds)
    {
        return knowledgeDocOperateMapper.deleteKnowledgeDocOperateByDocIds(docIds);
    }

    /**
     * 删除用户-文档操作关联信息
     * 
     * @param docId 用户-文档操作关联主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeDocOperateByDocId(String docId)
    {
        return knowledgeDocOperateMapper.deleteKnowledgeDocOperateByDocId(docId);
    }

    /**
     * 查询最近文档操作
     * @param noticer 用户id
     * @return 文档信息集合
     */
    @Override
    public List<String> selectKnowledgeDocOperateRecent(Long noticer, String attentionType) {
        // 最近10次关注行为（点赞或收藏）
        if ("like".equals(attentionType)) {
            return knowledgeDocOperateMapper.selectKnowledgeDocOperateRecent(noticer, "is_like");
        } else if ("collect".equals(attentionType)) {
            return knowledgeDocOperateMapper.selectKnowledgeDocOperateRecent(noticer, "is_collect");
        } else if ("download".equals(attentionType)) {
            return knowledgeDocOperateMapper.selectKnowledgeDocOperateRecent(noticer, "is_download");
        }
        return new ArrayList<>();
    }

    /**
     * 查询任一有操作的文档记录
     * @param userId 用户id
     * @return 文档信息集合
     */
    public List<String> selectKnowledgeDocOperateAnyDone(Long userId) {
        return knowledgeDocOperateMapper.selectKnowledgeDocOperateAnyDone(userId);
    }
}
